package com.ychs.lawyer.controller;
import com.ychs.lawyer.DTO.RoleMenu;
import com.ychs.lawyer.bean.Menu;
import com.ychs.lawyer.bean.R;
import com.ychs.lawyer.bean.Role;
import com.ychs.lawyer.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: clh
 * @Date: 2024/07/24/9:59
 * @Description:
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 获取除了按钮以外的菜单（一二级）
     * @param roleId
     * @return
     */
    @ApiOperation("通过角色获取有权限的菜单栏")
    @GetMapping("/getMenusByRoleId")
    public R<?> getMenusByRoleId(String roleId) {
        List<Menu> menus = menuService.getMenusByRoleId(roleId);
        return R.success(menus);
    }

    /**
     * 获取所有的菜单树包括按钮
     * @return
     */
    @ApiOperation("获取所有的菜单树包括按钮")
    @GetMapping("/list")
    public R<?> getMenuTree() {
        List<Menu> menuTree = menuService.getMenuTree();
        return R.success(menuTree);
    }

    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @ApiOperation("修改菜单")
    @PostMapping("/editMenu")
    public R<?> editMenu(@RequestBody Menu menu) {
        int res = menuService.editMenu(menu);
        return res > 0?R.success():R.fail();
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @ApiOperation("添加菜单")
    @PostMapping("/addMenu")
    public R<?> addMenu(@RequestBody Menu menu) {
        int res = menuService.addMenu(menu);
        return res > 0?R.success():R.fail();
    }

    /**
     * 通过roleId获取某个角色的可控菜单
     * @param roleMenu
     * @return
     */
    @ApiOperation("修改角色权限（回显该角色的可控菜单）")
    @PostMapping("/getMenuByRoleId")
    public R<?> getMenuByRoleId(@RequestBody RoleMenu roleMenu) {
        List<Integer> menuIds = menuService.getMenuByRoleId(roleMenu);
        return R.success(menuIds);
    }

    @ApiOperation("删除菜单")
    @GetMapping("/delMenu")
    public R<?> delMenu(Integer menuId) {
      System.out.println(menuId);
        log.info("!!!{}",menuId);
        int res = menuService.delMenu(menuId);
         return res>0 ?R.success():R.fail();
  }



}
